<?php
defined('BY_JYA') or exit('error');

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'文章列表'),
);
if($_RQ['op']=='list'){
	$fenlei=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>''),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'');
	$fenleiid=intval($_RQ['fenlei']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('id','paixu','title','fee','ishot','fenlei','pic_url','keyword','createtime','re_readnum','readnum','likenum','status','content');
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$order=intval($_RQ['order']);
	if($order==1){
		$order='id desc';
	}elseif($order==2){
		$order='ishot desc, id desc';
	}else{
		$order='paixu desc, id desc, readnum desc';
	}
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'',$order);
	if($list){
		foreach($list as &$item){
			$item['content'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
			$item['createtime']=date('y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['edit']=array('op'=>'edit','action'=>'account.article','query'=>'id='.$item['id']);
			$item['url']=custom_url('core/article.list/detail/id='.$item['id'],true);
			$item['allfee']=pdo_getsum('core_account_article_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'article_id'=>$item['id']),'fee');
		}
	}
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['list']=$list?$list:array();
	$return['fenlei']=$fenlei?$fenlei:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'fee','title'=>'费用','type'=>'string'),
			array('name'=>'allfee','title'=>'付费总额','type'=>'string'),
			array('name'=>'fenlei','title'=>'文章分类','type'=>'string'),
			array('name'=>'keyword','title'=>'关键字','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('title'=>'阅读量/点赞量','type'=>'img_string',
			'data'=>array(
				array('title'=>'真实阅读量：','name'=>'re_readnum'),
				array('title'=>'点赞量：','name'=>'likenum')
				)
			),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'ishot','title'=>'是否推荐','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('op'=>'bbs_release','title'=>'同步发帖到论坛','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','help'=>'','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择文章！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['paixu'])){
		$update['paixu']=$post['paixu'];
	}
	if(isset($post['ishot'])){
		$update['ishot']=$post['ishot'];
	}
	if(isset($post['status'])){
		$update['status']=$post['status'];
	}
	if($update){
		pdo_update('core_account_article',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='bbs_release'){
	//同步到论坛
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要同步的文章！','error');
	}
	$article=pdo_get('core_article',array('id'=>$id),array('title','content'));
	if(!$article){
		exi('指定文章不存在！','error');
	}
	$uc=$this->plugin_setting('basic');
	$uc=unserialize(cfc('tools')->base64_decode($uc));
	if(!$uc || !$uc['up_status'] || !$uc['db_host'] || !$uc['db_user'] || !$uc['db_passwd'] || !$uc['db_name'] || !$uc['db_charset'] || !$uc['bbs_pre'] || !$uc['up_cid'] || !$uc['up_name'] || !$uc['up_uid']){
		exi('UC参数配置有误，无法同步发帖。如需发帖，请前往基本设置——UC站点整合中配置好相关参数！','error');
	}
	$bbs_db=array(
		'host'=>$uc['db_host'],
		'username'=>$uc['db_user'],
		'password'=>$uc['db_passwd'],
		'database'=>$uc['db_name'],
		'charset'=>$uc['db_charset'],
		'tablepre'=>$uc['bbs_pre'],
		'port'=>3306,
	);
	require ROOT_D.'/addons/jy_ucenter/class/bbs_release.php';
	$bbs_release=new bbs_release($bbs_db);
	$res=$bbs_release->post($uc['up_cid'], $article['title'], $article['content'], $uc['up_name'], $uc['up_uid'],0);
	exi($res,'','hold');
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	$news=pdo_get('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$news['content'] = strip_tags(str_replace('&nbsp;','',$news['content']));
	$news['desc'] = $news['desc'] ? $news['desc'] : cfc('tools')->substr($news['content'],0,36);
	$news['url'] =custom_url('core/article.list/detail/id='.$news['id'],true);
	if($post['jy_toutiao'] && check_permission('jy_toutiao')){
		cfc('jy_toutiao')->new_article($news['title'],$news['content'],$news['desc'],1,$post['jy_toutiao_fennlei']);
	}
	if($post['jy_qier'] && check_permission('jy_qier')){
		cfc('jy_qier')->article($news['title'],$news['content'],tomedia($news['pic_url']),$post['jy_qier_fennlei']);
	}
	if($post['jy_baidu'] && check_permission('jy_baidu')){
		model('jy_baidu/set_data')->upnews($news['title'],$news['content'],$news['url'],tomedia($news['pic_url']));
	}
	if($post['jy_163'] && check_permission('jy_163')){
		model('jy_163/set_data')->upnews($news['title'],$news['content'],$post['jy_163_fennlei'],tomedia($news['pic_url']));
	}
	exi('发布成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_article',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}